<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>json_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_json_json_test.js.html">json_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2013 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.jsonTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.jsonTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.functions&#39;);
<a name="line19"></a>goog.require(&#39;goog.json&#39;);
<a name="line20"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line21"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line22"></a>
<a name="line23"></a>function allChars(start, end, opt_allowControlCharacters) {
<a name="line24"></a>  var sb = [];
<a name="line25"></a>  for (var i = start; i &lt; end; i++) {
<a name="line26"></a>    // unicode without the control characters 0x00 - 0x1f
<a name="line27"></a>    if (opt_allowControlCharacters || i &gt; 0x1f) {
<a name="line28"></a>      sb.push(String.fromCharCode(i));
<a name="line29"></a>    }
<a name="line30"></a>  }
<a name="line31"></a>  return sb.join(&#39;&#39;);
<a name="line32"></a>}
<a name="line33"></a>
<a name="line34"></a>// serialization
<a name="line35"></a>
<a name="line36"></a>function testStringSerialize() {
<a name="line37"></a>  assertSerialize(&#39;&quot;&quot;&#39;, &#39;&#39;);
<a name="line38"></a>
<a name="line39"></a>  // unicode
<a name="line40"></a>  var str = allChars(0, 10000);
<a name="line41"></a>  eval(goog.json.serialize(str));
<a name="line42"></a>
<a name="line43"></a>  assertSerialize(&#39;&quot;true&quot;&#39;, &#39;true&#39;);
<a name="line44"></a>  assertSerialize(&#39;&quot;false&quot;&#39;, &#39;false&#39;);
<a name="line45"></a>  assertSerialize(&#39;&quot;null&quot;&#39;, &#39;null&#39;);
<a name="line46"></a>  assertSerialize(&#39;&quot;0&quot;&#39;, &#39;0&#39;);
<a name="line47"></a>}
<a name="line48"></a>
<a name="line49"></a>function testNullSerialize() {
<a name="line50"></a>  assertSerialize(&#39;null&#39;, null);
<a name="line51"></a>  assertSerialize(&#39;null&#39;, undefined);
<a name="line52"></a>  assertSerialize(&#39;null&#39;, NaN);
<a name="line53"></a>
<a name="line54"></a>  assertSerialize(&#39;0&#39;, 0);
<a name="line55"></a>  assertSerialize(&#39;&quot;&quot;&#39;, &#39;&#39;);
<a name="line56"></a>  assertSerialize(&#39;false&#39;, false);
<a name="line57"></a>}
<a name="line58"></a>
<a name="line59"></a>function testNullPropertySerialize() {
<a name="line60"></a>  assertSerialize(&#39;{&quot;a&quot;:null}&#39;, {&#39;a&#39;: null});
<a name="line61"></a>  assertSerialize(&#39;{&quot;a&quot;:null}&#39;, {&#39;a&#39;: undefined});
<a name="line62"></a>}
<a name="line63"></a>
<a name="line64"></a>function testNumberSerialize() {
<a name="line65"></a>  assertSerialize(&#39;0&#39;, 0);
<a name="line66"></a>  assertSerialize(&#39;12345&#39;, 12345);
<a name="line67"></a>  assertSerialize(&#39;-12345&#39;, -12345);
<a name="line68"></a>
<a name="line69"></a>  assertSerialize(&#39;0.1&#39;, 0.1);
<a name="line70"></a>  // the leading zero may not be omitted
<a name="line71"></a>  assertSerialize(&#39;0.1&#39;, .1);
<a name="line72"></a>
<a name="line73"></a>  // no leading +
<a name="line74"></a>  assertSerialize(&#39;1&#39;, +1);
<a name="line75"></a>
<a name="line76"></a>  // either format is OK
<a name="line77"></a>  var s = goog.json.serialize(1e50);
<a name="line78"></a>  assertTrue(&#39;1e50&#39;,
<a name="line79"></a>      s == &#39;1e50&#39; || s == &#39;1E50&#39; ||
<a name="line80"></a>      s == &#39;1e+50&#39; || s == &#39;1E+50&#39;);
<a name="line81"></a>
<a name="line82"></a>  // either format is OK
<a name="line83"></a>  s = goog.json.serialize(1e-50);
<a name="line84"></a>  assertTrue(&#39;1e50&#39;, s == &#39;1e-50&#39; || s == &#39;1E-50&#39;);
<a name="line85"></a>
<a name="line86"></a>  // These numbers cannot be represented in JSON
<a name="line87"></a>  assertSerialize(&#39;null&#39;, NaN);
<a name="line88"></a>  assertSerialize(&#39;null&#39;, Infinity);
<a name="line89"></a>  assertSerialize(&#39;null&#39;, -Infinity);
<a name="line90"></a>}
<a name="line91"></a>
<a name="line92"></a>function testBooleanSerialize() {
<a name="line93"></a>  assertSerialize(&#39;true&#39;, true);
<a name="line94"></a>  assertSerialize(&#39;&quot;true&quot;&#39;, &#39;true&#39;);
<a name="line95"></a>
<a name="line96"></a>  assertSerialize(&#39;false&#39;, false);
<a name="line97"></a>  assertSerialize(&#39;&quot;false&quot;&#39;, &#39;false&#39;);
<a name="line98"></a>}
<a name="line99"></a>
<a name="line100"></a>function testArraySerialize() {
<a name="line101"></a>  assertSerialize(&#39;[]&#39;, []);
<a name="line102"></a>  assertSerialize(&#39;[1]&#39;, [1]);
<a name="line103"></a>  assertSerialize(&#39;[1,2]&#39;, [1, 2]);
<a name="line104"></a>  assertSerialize(&#39;[1,2,3]&#39;, [1, 2, 3]);
<a name="line105"></a>  assertSerialize(&#39;[[]]&#39;, [[]]);
<a name="line106"></a>
<a name="line107"></a>  assertNotEquals(&#39;{length:0}&#39;, goog.json.serialize({length: 0}), &#39;[]&#39;);
<a name="line108"></a>}
<a name="line109"></a>
<a name="line110"></a>function testObjectSerialize_emptyObject() {
<a name="line111"></a>  assertSerialize(&#39;{}&#39;, {});
<a name="line112"></a>}
<a name="line113"></a>
<a name="line114"></a>function testObjectSerialize_oneItem() {
<a name="line115"></a>  assertSerialize(&#39;{&quot;a&quot;:&quot;b&quot;}&#39;, {a: &#39;b&#39;});
<a name="line116"></a>}
<a name="line117"></a>
<a name="line118"></a>function testObjectSerialize_twoItems() {
<a name="line119"></a>  assertEquals(&#39;{&quot;a&quot;:&quot;b&quot;,&quot;c&quot;:&quot;d&quot;}&#39;,
<a name="line120"></a>               goog.json.serialize({a: &#39;b&#39;, c: &#39;d&#39;}),
<a name="line121"></a>               &#39;{&quot;a&quot;:&quot;b&quot;,&quot;c&quot;:&quot;d&quot;}&#39;);
<a name="line122"></a>}
<a name="line123"></a>
<a name="line124"></a>function testObjectSerialize_whitespace() {
<a name="line125"></a>  assertSerialize(&#39;{&quot; &quot;:&quot; &quot;}&#39;, {&#39; &#39;: &#39; &#39;});
<a name="line126"></a>}
<a name="line127"></a>
<a name="line128"></a>function testSerializeSkipFunction() {
<a name="line129"></a>  var object = {
<a name="line130"></a>    s: &#39;string value&#39;,
<a name="line131"></a>    b: true,
<a name="line132"></a>    i: 100,
<a name="line133"></a>    f: function() { var x = &#39;x&#39;; }
<a name="line134"></a>  };
<a name="line135"></a>  assertSerialize(&#39;&#39;, object.f);
<a name="line136"></a>  assertSerialize(&#39;{&quot;s&quot;:&quot;string value&quot;,&quot;b&quot;:true,&quot;i&quot;:100}&#39;, object);
<a name="line137"></a>}
<a name="line138"></a>
<a name="line139"></a>function testObjectSerialize_array() {
<a name="line140"></a>  assertNotEquals(&#39;[0,1]&#39;, goog.json.serialize([0, 1]), &#39;{&quot;0&quot;:&quot;0&quot;,&quot;1&quot;:&quot;1&quot;}&#39;);
<a name="line141"></a>}
<a name="line142"></a>
<a name="line143"></a>function testObjectSerialize_recursion() {
<a name="line144"></a>  if (goog.userAgent.WEBKIT) {
<a name="line145"></a>    return; // this makes safari 4 crash.
<a name="line146"></a>  }
<a name="line147"></a>
<a name="line148"></a>  var anObject = {};
<a name="line149"></a>  anObject.thisObject = anObject;
<a name="line150"></a>  assertThrows(&#39;expected recursion exception&#39;, function() {
<a name="line151"></a>    goog.json.serialize(anObject);
<a name="line152"></a>  });
<a name="line153"></a>}
<a name="line154"></a>
<a name="line155"></a>function testObjectSerializeWithHasOwnProperty() {
<a name="line156"></a>  var object = {&#39;hasOwnProperty&#39;: null};
<a name="line157"></a>  if (goog.userAgent.IE &amp;&amp; !goog.userAgent.isVersionOrHigher(&#39;9&#39;)) {
<a name="line158"></a>    assertEquals(&#39;{}&#39;, goog.json.serialize(object));
<a name="line159"></a>  } else {
<a name="line160"></a>    assertEquals(&#39;{&quot;hasOwnProperty&quot;:null}&#39;, goog.json.serialize(object));
<a name="line161"></a>  }
<a name="line162"></a>}
<a name="line163"></a>
<a name="line164"></a>// parsing
<a name="line165"></a>
<a name="line166"></a>function testStringParse() {
<a name="line167"></a>
<a name="line168"></a>  assertEquals(&#39;Empty string&#39;, goog.json.parse(&#39;&quot;&quot;&#39;), &#39;&#39;);
<a name="line169"></a>  assertEquals(&#39;whitespace string&#39;, goog.json.parse(&#39;&quot; &quot;&#39;), &#39; &#39;);
<a name="line170"></a>
<a name="line171"></a>  // unicode without the control characters 0x00 - 0x1f, 0x7f - 0x9f
<a name="line172"></a>  var str = allChars(0, 1000);
<a name="line173"></a>  var jsonString = goog.json.serialize(str);
<a name="line174"></a>  var a = eval(jsonString);
<a name="line175"></a>  assertEquals(&#39;unicode string&#39;, goog.json.parse(jsonString), a);
<a name="line176"></a>
<a name="line177"></a>  assertEquals(&#39;true as a string&#39;, goog.json.parse(&#39;&quot;true&quot;&#39;), &#39;true&#39;);
<a name="line178"></a>  assertEquals(&#39;false as a string&#39;, goog.json.parse(&#39;&quot;false&quot;&#39;), &#39;false&#39;);
<a name="line179"></a>  assertEquals(&#39;null as a string&#39;, goog.json.parse(&#39;&quot;null&quot;&#39;), &#39;null&#39;);
<a name="line180"></a>  assertEquals(&#39;number as a string&#39;, goog.json.parse(&#39;&quot;0&quot;&#39;), &#39;0&#39;);
<a name="line181"></a>}
<a name="line182"></a>
<a name="line183"></a>function testStringUnsafeParse() {
<a name="line184"></a>
<a name="line185"></a>  assertEquals(&#39;Empty string&#39;, goog.json.unsafeParse(&#39;&quot;&quot;&#39;), &#39;&#39;);
<a name="line186"></a>  assertEquals(&#39;whitespace string&#39;, goog.json.unsafeParse(&#39;&quot; &quot;&#39;), &#39; &#39;);
<a name="line187"></a>
<a name="line188"></a>  // unicode
<a name="line189"></a>  var str = allChars(0, 1000);
<a name="line190"></a>  var jsonString = goog.json.serialize(str);
<a name="line191"></a>  var a = eval(jsonString);
<a name="line192"></a>  assertEquals(&#39;unicode string&#39;, goog.json.unsafeParse(jsonString), a);
<a name="line193"></a>
<a name="line194"></a>  assertEquals(&#39;true as a string&#39;, goog.json.unsafeParse(&#39;&quot;true&quot;&#39;), &#39;true&#39;);
<a name="line195"></a>  assertEquals(&#39;false as a string&#39;, goog.json.unsafeParse(&#39;&quot;false&quot;&#39;), &#39;false&#39;);
<a name="line196"></a>  assertEquals(&#39;null as a string&#39;, goog.json.unsafeParse(&#39;&quot;null&quot;&#39;), &#39;null&#39;);
<a name="line197"></a>  assertEquals(&#39;number as a string&#39;, goog.json.unsafeParse(&#39;&quot;0&quot;&#39;), &#39;0&#39;);
<a name="line198"></a>}
<a name="line199"></a>
<a name="line200"></a>function testNullParse() {
<a name="line201"></a>  assertEquals(&#39;null&#39;, goog.json.parse(null), null);
<a name="line202"></a>  assertEquals(&#39;null&#39;, goog.json.parse(&#39;null&#39;), null);
<a name="line203"></a>
<a name="line204"></a>  assertNotEquals(&#39;0&#39;, goog.json.parse(&#39;0&#39;), null);
<a name="line205"></a>  assertNotEquals(&#39;&quot;&quot;&#39;, goog.json.parse(&#39;&quot;&quot;&#39;), null);
<a name="line206"></a>  assertNotEquals(&#39;false&#39;, goog.json.parse(&#39;false&#39;), null);
<a name="line207"></a>}
<a name="line208"></a>
<a name="line209"></a>function testNullUnsafeParse() {
<a name="line210"></a>  assertEquals(&#39;null&#39;, goog.json.unsafeParse(null), null);
<a name="line211"></a>  assertEquals(&#39;null&#39;, goog.json.unsafeParse(&#39;null&#39;), null);
<a name="line212"></a>
<a name="line213"></a>  assertNotEquals(&#39;0&#39;, goog.json.unsafeParse(&#39;0&#39;), null);
<a name="line214"></a>  assertNotEquals(&#39;&quot;&quot;&#39;, goog.json.unsafeParse(&#39;&quot;&quot;&#39;), null);
<a name="line215"></a>  assertNotEquals(&#39;false&#39;, goog.json.unsafeParse(&#39;false&#39;), null);
<a name="line216"></a>}
<a name="line217"></a>
<a name="line218"></a>function testNumberParse() {
<a name="line219"></a>  assertEquals(&#39;0&#39;, goog.json.parse(&#39;0&#39;), 0);
<a name="line220"></a>  assertEquals(&#39;12345&#39;, goog.json.parse(&#39;12345&#39;), 12345);
<a name="line221"></a>  assertEquals(&#39;-12345&#39;, goog.json.parse(&#39;-12345&#39;), -12345);
<a name="line222"></a>
<a name="line223"></a>  assertEquals(&#39;0.1&#39;, goog.json.parse(&#39;0.1&#39;), 0.1);
<a name="line224"></a>
<a name="line225"></a>  // either format is OK
<a name="line226"></a>  assertEquals(1e50, goog.json.parse(&#39;1e50&#39;));
<a name="line227"></a>  assertEquals(1e50, goog.json.parse(&#39;1E50&#39;));
<a name="line228"></a>  assertEquals(1e50, goog.json.parse(&#39;1e+50&#39;));
<a name="line229"></a>  assertEquals(1e50, goog.json.parse(&#39;1E+50&#39;));
<a name="line230"></a>
<a name="line231"></a>  // either format is OK
<a name="line232"></a>  assertEquals(1e-50, goog.json.parse(&#39;1e-50&#39;));
<a name="line233"></a>  assertEquals(1e-50, goog.json.parse(&#39;1E-50&#39;));
<a name="line234"></a>}
<a name="line235"></a>
<a name="line236"></a>function testNumberUnsafeParse() {
<a name="line237"></a>  assertEquals(&#39;0&#39;, goog.json.unsafeParse(&#39;0&#39;), 0);
<a name="line238"></a>  assertEquals(&#39;12345&#39;, goog.json.unsafeParse(&#39;12345&#39;), 12345);
<a name="line239"></a>  assertEquals(&#39;-12345&#39;, goog.json.unsafeParse(&#39;-12345&#39;), -12345);
<a name="line240"></a>
<a name="line241"></a>  assertEquals(&#39;0.1&#39;, goog.json.unsafeParse(&#39;0.1&#39;), 0.1);
<a name="line242"></a>
<a name="line243"></a>  // either format is OK
<a name="line244"></a>  assertEquals(1e50, goog.json.unsafeParse(&#39;1e50&#39;));
<a name="line245"></a>  assertEquals(1e50, goog.json.unsafeParse(&#39;1E50&#39;));
<a name="line246"></a>  assertEquals(1e50, goog.json.unsafeParse(&#39;1e+50&#39;));
<a name="line247"></a>  assertEquals(1e50, goog.json.unsafeParse(&#39;1E+50&#39;));
<a name="line248"></a>
<a name="line249"></a>  // either format is OK
<a name="line250"></a>  assertEquals(1e-50, goog.json.unsafeParse(&#39;1e-50&#39;));
<a name="line251"></a>  assertEquals(1e-50, goog.json.unsafeParse(&#39;1E-50&#39;));
<a name="line252"></a>}
<a name="line253"></a>
<a name="line254"></a>function testBooleanParse() {
<a name="line255"></a>  assertEquals(&#39;true&#39;, goog.json.parse(&#39;true&#39;), true);
<a name="line256"></a>  assertEquals(&#39;false&#39;, goog.json.parse(&#39;false&#39;), false);
<a name="line257"></a>
<a name="line258"></a>  assertNotEquals(&#39;0&#39;, goog.json.parse(&#39;0&#39;), false);
<a name="line259"></a>  assertNotEquals(&#39;&quot;false&quot;&#39;, goog.json.parse(&#39;&quot;false&quot;&#39;), false);
<a name="line260"></a>  assertNotEquals(&#39;null&#39;, goog.json.parse(&#39;null&#39;), false);
<a name="line261"></a>
<a name="line262"></a>  assertNotEquals(&#39;1&#39;, goog.json.parse(&#39;1&#39;), true);
<a name="line263"></a>  assertNotEquals(&#39;&quot;true&quot;&#39;, goog.json.parse(&#39;&quot;true&quot;&#39;), true);
<a name="line264"></a>  assertNotEquals(&#39;{}&#39;, goog.json.parse(&#39;{}&#39;), true);
<a name="line265"></a>  assertNotEquals(&#39;[]&#39;, goog.json.parse(&#39;[]&#39;), true);
<a name="line266"></a>}
<a name="line267"></a>
<a name="line268"></a>function testBooleanUnsafeParse() {
<a name="line269"></a>  assertEquals(&#39;true&#39;, goog.json.unsafeParse(&#39;true&#39;), true);
<a name="line270"></a>  assertEquals(&#39;false&#39;, goog.json.unsafeParse(&#39;false&#39;), false);
<a name="line271"></a>
<a name="line272"></a>  assertNotEquals(&#39;0&#39;, goog.json.unsafeParse(&#39;0&#39;), false);
<a name="line273"></a>  assertNotEquals(&#39;&quot;false&quot;&#39;, goog.json.unsafeParse(&#39;&quot;false&quot;&#39;), false);
<a name="line274"></a>  assertNotEquals(&#39;null&#39;, goog.json.unsafeParse(&#39;null&#39;), false);
<a name="line275"></a>
<a name="line276"></a>  assertNotEquals(&#39;1&#39;, goog.json.unsafeParse(&#39;1&#39;), true);
<a name="line277"></a>  assertNotEquals(&#39;&quot;true&quot;&#39;, goog.json.unsafeParse(&#39;&quot;true&quot;&#39;), true);
<a name="line278"></a>  assertNotEquals(&#39;{}&#39;, goog.json.unsafeParse(&#39;{}&#39;), true);
<a name="line279"></a>  assertNotEquals(&#39;[]&#39;, goog.json.unsafeParse(&#39;[]&#39;), true);
<a name="line280"></a>}
<a name="line281"></a>
<a name="line282"></a>function testArrayParse() {
<a name="line283"></a>  function arrayEquals(a1, a2) {
<a name="line284"></a>    if (a1.length != a2.length) {
<a name="line285"></a>      return false;
<a name="line286"></a>    }
<a name="line287"></a>    for (var i = 0; i &lt; a1.length; i++) {
<a name="line288"></a>      if (a1[i] != a2[i]) {
<a name="line289"></a>        return false;
<a name="line290"></a>      }
<a name="line291"></a>    }
<a name="line292"></a>    return true;
<a name="line293"></a>  }
<a name="line294"></a>
<a name="line295"></a>  assertTrue(&#39;[]&#39;, arrayEquals(goog.json.parse(&#39;[]&#39;), []));
<a name="line296"></a>  assertTrue(&#39;[1]&#39;, arrayEquals(goog.json.parse(&#39;[1]&#39;), [1]));
<a name="line297"></a>  assertTrue(&#39;[1,2]&#39;, arrayEquals(goog.json.parse(&#39;[1,2]&#39;), [1, 2]));
<a name="line298"></a>  assertTrue(&#39;[1,2,3]&#39;, arrayEquals(goog.json.parse(&#39;[1,2,3]&#39;), [1, 2, 3]));
<a name="line299"></a>  assertTrue(&#39;[[]]&#39;, arrayEquals(goog.json.parse(&#39;[[]]&#39;)[0], []));
<a name="line300"></a>
<a name="line301"></a>  // make sure we do not get an array for something that looks like an array
<a name="line302"></a>  assertFalse(&#39;{length:0}&#39;, &#39;push&#39; in goog.json.parse(&#39;{&quot;length&quot;:0}&#39;));
<a name="line303"></a>}
<a name="line304"></a>
<a name="line305"></a>function testArrayUnsafeParse() {
<a name="line306"></a>  function arrayEquals(a1, a2) {
<a name="line307"></a>    if (a1.length != a2.length) {
<a name="line308"></a>      return false;
<a name="line309"></a>    }
<a name="line310"></a>    for (var i = 0; i &lt; a1.length; i++) {
<a name="line311"></a>      if (a1[i] != a2[i]) {
<a name="line312"></a>        return false;
<a name="line313"></a>      }
<a name="line314"></a>    }
<a name="line315"></a>    return true;
<a name="line316"></a>  }
<a name="line317"></a>
<a name="line318"></a>  assertTrue(&#39;[]&#39;, arrayEquals(goog.json.unsafeParse(&#39;[]&#39;), []));
<a name="line319"></a>  assertTrue(&#39;[1]&#39;, arrayEquals(goog.json.unsafeParse(&#39;[1]&#39;), [1]));
<a name="line320"></a>  assertTrue(&#39;[1,2]&#39;, arrayEquals(goog.json.unsafeParse(&#39;[1,2]&#39;), [1, 2]));
<a name="line321"></a>  assertTrue(&#39;[1,2,3]&#39;,
<a name="line322"></a>      arrayEquals(goog.json.unsafeParse(&#39;[1,2,3]&#39;), [1, 2, 3]));
<a name="line323"></a>  assertTrue(&#39;[[]]&#39;, arrayEquals(goog.json.unsafeParse(&#39;[[]]&#39;)[0], []));
<a name="line324"></a>
<a name="line325"></a>  // make sure we do not get an array for something that looks like an array
<a name="line326"></a>  assertFalse(&#39;{length:0}&#39;, &#39;push&#39; in goog.json.unsafeParse(&#39;{&quot;length&quot;:0}&#39;));
<a name="line327"></a>}
<a name="line328"></a>
<a name="line329"></a>function testObjectParse() {
<a name="line330"></a>  function objectEquals(a1, a2) {
<a name="line331"></a>    for (var key in a1) {
<a name="line332"></a>      if (a1[key] != a2[key]) {
<a name="line333"></a>        return false;
<a name="line334"></a>      }
<a name="line335"></a>    }
<a name="line336"></a>    return true;
<a name="line337"></a>  }
<a name="line338"></a>
<a name="line339"></a>  assertTrue(&#39;{}&#39;, objectEquals(goog.json.parse(&#39;{}&#39;), {}));
<a name="line340"></a>  assertTrue(&#39;{&quot;a&quot;:&quot;b&quot;}&#39;,
<a name="line341"></a>      objectEquals(goog.json.parse(&#39;{&quot;a&quot;:&quot;b&quot;}&#39;), {a: &#39;b&#39;}));
<a name="line342"></a>  assertTrue(&#39;{&quot;a&quot;:&quot;b&quot;,&quot;c&quot;:&quot;d&quot;}&#39;,
<a name="line343"></a>             objectEquals(goog.json.parse(&#39;{&quot;a&quot;:&quot;b&quot;,&quot;c&quot;:&quot;d&quot;}&#39;),
<a name="line344"></a>             {a: &#39;b&#39;, c: &#39;d&#39;}));
<a name="line345"></a>  assertTrue(&#39;{&quot; &quot;:&quot; &quot;}&#39;,
<a name="line346"></a>      objectEquals(goog.json.parse(&#39;{&quot; &quot;:&quot; &quot;}&#39;), {&#39; &#39;: &#39; &#39;}));
<a name="line347"></a>
<a name="line348"></a>  // make sure we do not get an Object when it is really an array
<a name="line349"></a>  assertTrue(&#39;[0,1]&#39;, &#39;length&#39; in goog.json.parse(&#39;[0,1]&#39;));
<a name="line350"></a>}
<a name="line351"></a>
<a name="line352"></a>function testObjectUnsafeParse() {
<a name="line353"></a>  function objectEquals(a1, a2) {
<a name="line354"></a>    for (var key in a1) {
<a name="line355"></a>      if (a1[key] != a2[key]) {
<a name="line356"></a>        return false;
<a name="line357"></a>      }
<a name="line358"></a>    }
<a name="line359"></a>    return true;
<a name="line360"></a>  }
<a name="line361"></a>
<a name="line362"></a>  assertTrue(&#39;{}&#39;, objectEquals(goog.json.unsafeParse(&#39;{}&#39;), {}));
<a name="line363"></a>  assertTrue(&#39;{&quot;a&quot;:&quot;b&quot;}&#39;,
<a name="line364"></a>      objectEquals(goog.json.unsafeParse(&#39;{&quot;a&quot;:&quot;b&quot;}&#39;), {a: &#39;b&#39;}));
<a name="line365"></a>  assertTrue(&#39;{&quot;a&quot;:&quot;b&quot;,&quot;c&quot;:&quot;d&quot;}&#39;,
<a name="line366"></a>             objectEquals(goog.json.unsafeParse(&#39;{&quot;a&quot;:&quot;b&quot;,&quot;c&quot;:&quot;d&quot;}&#39;),
<a name="line367"></a>             {a: &#39;b&#39;, c: &#39;d&#39;}));
<a name="line368"></a>  assertTrue(&#39;{&quot; &quot;:&quot; &quot;}&#39;,
<a name="line369"></a>      objectEquals(goog.json.unsafeParse(&#39;{&quot; &quot;:&quot; &quot;}&#39;), {&#39; &#39;: &#39; &#39;}));
<a name="line370"></a>
<a name="line371"></a>  // make sure we do not get an Object when it is really an array
<a name="line372"></a>  assertTrue(&#39;[0,1]&#39;, &#39;length&#39; in goog.json.unsafeParse(&#39;[0,1]&#39;));
<a name="line373"></a>}
<a name="line374"></a>
<a name="line375"></a>
<a name="line376"></a>function testForValidJson() {
<a name="line377"></a>  function error_(msg, s) {
<a name="line378"></a>    assertThrows(msg + &#39;, Should have raised an exception: &#39; + s,
<a name="line379"></a>        goog.partial(goog.json.parse, s));
<a name="line380"></a>  }
<a name="line381"></a>
<a name="line382"></a>  error_(&#39;Non closed string&#39;, &#39;&quot;dasdas&#39;);
<a name="line383"></a>  error_(&#39;undefined is not valid json&#39;, &#39;undefined&#39;);
<a name="line384"></a>
<a name="line385"></a>  // These numbers cannot be represented in JSON
<a name="line386"></a>  error_(&#39;NaN cannot be presented in JSON&#39;, &#39;NaN&#39;);
<a name="line387"></a>  error_(&#39;Infinity cannot be presented in JSON&#39;, &#39;Infinity&#39;);
<a name="line388"></a>  error_(&#39;-Infinity cannot be presented in JSON&#39;, &#39;-Infinity&#39;);
<a name="line389"></a>}
<a name="line390"></a>
<a name="line391"></a>function testIsNotValid() {
<a name="line392"></a>  assertFalse(goog.json.isValid_(&#39;t&#39;));
<a name="line393"></a>  assertFalse(goog.json.isValid_(&#39;r&#39;));
<a name="line394"></a>  assertFalse(goog.json.isValid_(&#39;u&#39;));
<a name="line395"></a>  assertFalse(goog.json.isValid_(&#39;e&#39;));
<a name="line396"></a>  assertFalse(goog.json.isValid_(&#39;f&#39;));
<a name="line397"></a>  assertFalse(goog.json.isValid_(&#39;a&#39;));
<a name="line398"></a>  assertFalse(goog.json.isValid_(&#39;l&#39;));
<a name="line399"></a>  assertFalse(goog.json.isValid_(&#39;s&#39;));
<a name="line400"></a>  assertFalse(goog.json.isValid_(&#39;n&#39;));
<a name="line401"></a>  assertFalse(goog.json.isValid_(&#39;E&#39;));
<a name="line402"></a>
<a name="line403"></a>  assertFalse(goog.json.isValid_(&#39;+&#39;));
<a name="line404"></a>  assertFalse(goog.json.isValid_(&#39;-&#39;));
<a name="line405"></a>
<a name="line406"></a>  assertFalse(goog.json.isValid_(&#39;t++&#39;));
<a name="line407"></a>  assertFalse(goog.json.isValid_(&#39;++t&#39;));
<a name="line408"></a>  assertFalse(goog.json.isValid_(&#39;t--&#39;));
<a name="line409"></a>  assertFalse(goog.json.isValid_(&#39;--t&#39;));
<a name="line410"></a>  assertFalse(goog.json.isValid_(&#39;-t&#39;));
<a name="line411"></a>  assertFalse(goog.json.isValid_(&#39;+t&#39;));
<a name="line412"></a>
<a name="line413"></a>  assertFalse(goog.json.isValid_(&#39;&quot;\\&quot;&#39;)); // &quot;\&quot;
<a name="line414"></a>  assertFalse(goog.json.isValid_(&#39;&quot;\\&#39;));  // &quot;\
<a name="line415"></a>
<a name="line416"></a>  // multiline string using \ at the end is not valid
<a name="line417"></a>  assertFalse(goog.json.isValid_(&#39;&quot;a\\\nb&quot;&#39;));
<a name="line418"></a>
<a name="line419"></a>
<a name="line420"></a>  assertFalse(goog.json.isValid_(&#39;&quot;\n&quot;&#39;));
<a name="line421"></a>  assertFalse(goog.json.isValid_(&#39;&quot;\r&quot;&#39;));
<a name="line422"></a>  assertFalse(goog.json.isValid_(&#39;&quot;\r\n&quot;&#39;));
<a name="line423"></a>  // Disallow the unicode newlines
<a name="line424"></a>  assertFalse(goog.json.isValid_(&#39;&quot;\u2028&quot;&#39;));
<a name="line425"></a>  assertFalse(goog.json.isValid_(&#39;&quot;\u2029&quot;&#39;));
<a name="line426"></a>
<a name="line427"></a>  assertFalse(goog.json.isValid_(&#39; &#39;));
<a name="line428"></a>  assertFalse(goog.json.isValid_(&#39;\n&#39;));
<a name="line429"></a>  assertFalse(goog.json.isValid_(&#39;\r&#39;));
<a name="line430"></a>  assertFalse(goog.json.isValid_(&#39;\r\n&#39;));
<a name="line431"></a>
<a name="line432"></a>  assertFalse(goog.json.isValid_(&#39;t.r&#39;));
<a name="line433"></a>
<a name="line434"></a>  assertFalse(goog.json.isValid_(&#39;1e&#39;));
<a name="line435"></a>  assertFalse(goog.json.isValid_(&#39;1e-&#39;));
<a name="line436"></a>  assertFalse(goog.json.isValid_(&#39;1e+&#39;));
<a name="line437"></a>
<a name="line438"></a>  assertFalse(goog.json.isValid_(&#39;1e-&#39;));
<a name="line439"></a>
<a name="line440"></a>  assertFalse(goog.json.isValid_(&#39;&quot;\\\u200D\\&quot;&#39;));
<a name="line441"></a>  assertFalse(goog.json.isValid_(&#39;&quot;\\\0\\&quot;&#39;));
<a name="line442"></a>  assertFalse(goog.json.isValid_(&#39;&quot;\\\0&quot;&#39;));
<a name="line443"></a>  assertFalse(goog.json.isValid_(&#39;&quot;\\0&quot;&#39;));
<a name="line444"></a>  assertFalse(goog.json.isValid_(&#39;&quot;\x0c&quot;&#39;));
<a name="line445"></a>
<a name="line446"></a>  assertFalse(goog.json.isValid_(&#39;&quot;\\\u200D\\&quot;, alert(\&#39;foo\&#39;) //&quot;\n&#39;));
<a name="line447"></a>}
<a name="line448"></a>
<a name="line449"></a>function testIsValid() {
<a name="line450"></a>  assertTrue(goog.json.isValid_(&#39;\n&quot;&quot;\n&#39;));
<a name="line451"></a>  assertTrue(goog.json.isValid_(&#39;[1\n,2\r,3\u2028\n,4\u2029]&#39;));
<a name="line452"></a>  assertTrue(goog.json.isValid_(&#39;&quot;\x7f&quot;&#39;));
<a name="line453"></a>  assertTrue(goog.json.isValid_(&#39;&quot;\x09&quot;&#39;));
<a name="line454"></a>  // Test tab characters in json.
<a name="line455"></a>  assertTrue(goog.json.isValid_(&#39;{&quot;\t&quot;:&quot;\t&quot;}&#39;));
<a name="line456"></a>}
<a name="line457"></a>
<a name="line458"></a>function testDoNotSerializeProto() {
<a name="line459"></a>  function F() {};
<a name="line460"></a>  F.prototype = {
<a name="line461"></a>    c: 3
<a name="line462"></a>  };
<a name="line463"></a>
<a name="line464"></a>  var obj = new F;
<a name="line465"></a>  obj.a = 1;
<a name="line466"></a>  obj.b = 2;
<a name="line467"></a>
<a name="line468"></a>  assertEquals(&#39;Should not follow the prototype chain&#39;,
<a name="line469"></a>               &#39;{&quot;a&quot;:1,&quot;b&quot;:2}&#39;,
<a name="line470"></a>               goog.json.serialize(obj));
<a name="line471"></a>}
<a name="line472"></a>
<a name="line473"></a>function testEscape() {
<a name="line474"></a>  var unescaped = &#39;1a*/]&#39;;
<a name="line475"></a>  assertEquals(&#39;Should not escape&#39;,
<a name="line476"></a>               &#39;&quot;&#39; + unescaped + &#39;&quot;&#39;,
<a name="line477"></a>               goog.json.serialize(unescaped));
<a name="line478"></a>
<a name="line479"></a>  var escaped = &#39;\n\x7f\u1049&#39;;
<a name="line480"></a>  assertEquals(&#39;Should escape&#39;,
<a name="line481"></a>               &#39;&#39;,
<a name="line482"></a>               findCommonChar(escaped, goog.json.serialize(escaped)));
<a name="line483"></a>  assertEquals(&#39;Should eval to the same string after escaping&#39;,
<a name="line484"></a>               escaped,
<a name="line485"></a>               goog.json.parse(goog.json.serialize(escaped)));
<a name="line486"></a>}
<a name="line487"></a>
<a name="line488"></a>function testReplacer() {
<a name="line489"></a>  assertSerialize(&#39;[null,null,0]&#39;, [, , 0]);
<a name="line490"></a>
<a name="line491"></a>  assertSerialize(&#39;[0,0,{&quot;x&quot;:0}]&#39;, [, , {x: 0}], function(k, v) {
<a name="line492"></a>    if (v === undefined &amp;&amp; goog.isArray(this)) {
<a name="line493"></a>      return 0;
<a name="line494"></a>    }
<a name="line495"></a>    return v;
<a name="line496"></a>  });
<a name="line497"></a>
<a name="line498"></a>  assertSerialize(&#39;[0,1,2,3]&#39;, [0, 0, 0, 0], function(k, v) {
<a name="line499"></a>    var kNum = Number(k);
<a name="line500"></a>    if (k &amp;&amp; !isNaN(kNum)) {
<a name="line501"></a>      return kNum;
<a name="line502"></a>    }
<a name="line503"></a>    return v;
<a name="line504"></a>  });
<a name="line505"></a>
<a name="line506"></a>  var f = function(k, v) {
<a name="line507"></a>    return typeof v == &#39;number&#39; ? v + 1 : v;
<a name="line508"></a>  };
<a name="line509"></a>  assertSerialize(&#39;{&quot;a&quot;:1,&quot;b&quot;:{&quot;c&quot;:2}}&#39;, {&#39;a&#39;: 0, &#39;b&#39;: {&#39;c&#39;: 1}}, f);
<a name="line510"></a>}
<a name="line511"></a>
<a name="line512"></a>function testDateSerialize() {
<a name="line513"></a>  assertSerialize(&#39;{}&#39;, new Date(0));
<a name="line514"></a>}
<a name="line515"></a>
<a name="line516"></a>function testToJSONSerialize() {
<a name="line517"></a>  assertSerialize(&#39;{}&#39;, {toJSON: goog.functions.constant(&#39;serialized&#39;)});
<a name="line518"></a>  assertSerialize(&#39;{&quot;toJSON&quot;:&quot;normal&quot;}&#39;, {toJSON: &#39;normal&#39;});
<a name="line519"></a>}
<a name="line520"></a>
<a name="line521"></a>
<a name="line522"></a>/**
<a name="line523"></a> * Asserts that the given object serializes to the given value.
<a name="line524"></a> * If the current browser has an implementation of JSON.serialize,
<a name="line525"></a> * we make sure our version matches that one.
<a name="line526"></a> */
<a name="line527"></a>function assertSerialize(expected, obj, opt_replacer) {
<a name="line528"></a>  assertEquals(expected, goog.json.serialize(obj, opt_replacer));
<a name="line529"></a>
<a name="line530"></a>  // I&#39;m pretty sure that the goog.json.serialize behavior is correct by the ES5
<a name="line531"></a>  // spec, but JSON.stringify(undefined) is undefined on all browsers.
<a name="line532"></a>  if (obj === undefined) return;
<a name="line533"></a>
<a name="line534"></a>  // Browsers don&#39;t serialize undefined properties, but goog.json.serialize does
<a name="line535"></a>  if (goog.isObject(obj) &amp;&amp; (&#39;a&#39; in obj) &amp;&amp; obj[&#39;a&#39;] === undefined) return;
<a name="line536"></a>
<a name="line537"></a>  // Replacers are broken on IE and older versions of firefox.
<a name="line538"></a>  if (opt_replacer &amp;&amp; !goog.userAgent.WEBKIT) return;
<a name="line539"></a>
<a name="line540"></a>  // goog.json.serialize does not stringify dates the same way.
<a name="line541"></a>  if (obj instanceof Date) return;
<a name="line542"></a>
<a name="line543"></a>  // goog.json.serialize does not stringify functions the same way.
<a name="line544"></a>  if (obj instanceof Function) return;
<a name="line545"></a>
<a name="line546"></a>  // goog.json.serialize doesn&#39;t use the toJSON method.
<a name="line547"></a>  if (goog.isObject(obj) &amp;&amp; goog.isFunction(obj.toJSON)) return;
<a name="line548"></a>
<a name="line549"></a>  if (typeof JSON != &#39;undefined&#39;) {
<a name="line550"></a>    assertEquals(
<a name="line551"></a>        &#39;goog.json.serialize does not match JSON.stringify&#39;,
<a name="line552"></a>        expected,
<a name="line553"></a>        JSON.stringify(obj, opt_replacer));
<a name="line554"></a>  }
<a name="line555"></a>}
<a name="line556"></a>
<a name="line557"></a>
<a name="line558"></a>/**
<a name="line559"></a> * @param {string} a
<a name="line560"></a> * @param {string} b
<a name="line561"></a> * @return {string} any common character between two strings a and b.
<a name="line562"></a> */
<a name="line563"></a>function findCommonChar(a, b) {
<a name="line564"></a>  for (var i = 0; i &lt; b.length; i++) {
<a name="line565"></a>    if (a.indexOf(b.charAt(i)) &gt;= 0) {
<a name="line566"></a>      return b.charAt(i);
<a name="line567"></a>    }
<a name="line568"></a>  }
<a name="line569"></a>  return &#39;&#39;;
<a name="line570"></a>}
</pre>


</body>
</html>
